# -*- coding:utf-8 -*-
# @Time : 21-9-27 上午11:08 
# @Author : zdy
# @File : UIBalance.py
# desc:
from UI.Balance import Ui_Balance
from Class.PortManager import portManager
import serial
from serial import PARITY_NONE, PARITY_ODD, PARITY_EVEN
from importQT import *
from Class.Devices import *

class UIBalance(QDialog, Ui_Balance):
    def __init__(self):
        super(UIBalance, self).__init__()
        self.setupUi(self)
        self.setWindowFlags(Qt.WindowCloseButtonHint)
        self.setFixedSize(self.width(), self.height())
        self.applyResource()
        port_list = list(serial.tools.list_ports.comports())
        for port in port_list:
            if port[0].__contains__('tty'):
                self.port_cbb.addItem(port[0])
        self.port_cbb.addItem(portManager.portName)

        self.boud_cbb.addItem('115200')
        self.boud_cbb.addItem('19200')
        self.boud_cbb.addItem('9600')
        self.boud_cbb.addItem('1200')
        self.boud_cbb.setCurrentText(str(portManager.baudRate))
        # 数据位控件
        self.data_cbb.addItem('8')
        self.data_cbb.addItem('7')
        self.data_cbb.addItem('6')
        self.data_cbb.addItem('5')
        self.data_cbb.setCurrentText(str(portManager.dataBits))
        # 停止位控件
        self.stop_cbb.addItem('1')
        self.stop_cbb.addItem('1.5')
        self.stop_cbb.addItem('2')
        self.stop_cbb.setCurrentText(str(portManager.stopBits))
        # 校验位控件
        self.parity_cbb.addItem(PARITY_NONE)
        self.parity_cbb.addItem(PARITY_ODD)
        self.parity_cbb.addItem(PARITY_EVEN)
        self.parity_cbb.setCurrentText(str(portManager.parity))

        self.txtReceive.document().setMaximumBlockCount(20)

        if portManager.isOpened():
            self.btnTest.setText("停止测试")
            self.refresh = True  # 刷新界面显示
        else:
            self.btnTest.setText("开始测试")
            self.refresh = False
        portManager.sigReceive.connect(self.msgREceived)
        self.btnSet.clicked.connect(self.btnSet_clicked)
        self.btnTest.clicked.connect(self.btnTest_clicked)

    def applyResource(self):
        self.setWindowIcon(getIcon())

    def msgREceived(self, msg):
        if self.refresh:
            self.txtReceive.append(msg)
            self.txtReceive.moveCursor(self.txtReceive.textCursor().End)  # 文本框显示到底部

    def btnSet_clicked(self):
        if portManager.isOpened():
            portManager.close()
        portManager.portName = self.port_cbb.currentText()
        portManager.baudRate = int(self.boud_cbb.currentText())
        portManager.dataBits = int(self.data_cbb.currentText())
        portManager.stopBits = int(self.stop_cbb.currentText())
        portManager.parity = self.parity_cbb.currentText()
        portManager.setParams()

    def btnTest_clicked(self):
        if self.refresh:
            self.refresh = False
            self.btnTest.setText("开始测试")
        else:
            res = portManager.open()
            if isinstance(res, bool):
                if not res:
                    QMessageBox.warning(self,"提示", "天平链接失败")
                else:
                    self.refresh = True
                    self.btnTest.setText("停止测试")
            else:
                QMessageBox.warning(self,"提示", res)


    def closeEvent(self, event):
        portManager.sigReceive.disconnect(self.msgREceived)






